﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

public partial class AppCode_Admin_AddEmployee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SetVisible();
        if (!IsPostBack)
        {
            if (Session["userNameAdmin"] == null)
            {
                Response.Redirect("..\\Home\\HomePage.aspx");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Boolean check = false;

        if (txtDesignation.Text.Equals(""))
        {
            Label2.Visible = true;
            check = true;
        }
        if (txtJoinDate.Text.Equals(""))
        {
            Label4.Visible = true;
            check = true;
        }
        if (txtSalary.Text.Equals(""))
        {
            Label6.Visible = true;
            check = true;
        }
        else
        {
            try
            {
                decimal a = Convert.ToDecimal(txtSalary.Text);
            }
            catch
            {
                Label26.Visible = true;
                check = true;
            }
        }
        if (txtFirstName.Text.Equals(""))
        {
            Label8.Visible = true;
            check = true;
        }
        if (txtLastName.Text.Equals(""))
        {
            Label10.Visible = true;
            check = true;
        }
        if (txtUserName.Text.Equals(""))
        {
            Label12.Visible = true;
            check = true;
        }
        else if (CheckExistingUser(txtUserName.Text))
        {
            Label25.Visible = true;
            check = true;
        }

        if (txtPassword.Text.Equals(""))
        {
            Label14.Visible = true;
            check = true;
        }
        if (txtAddress.Text.Equals(""))
        {
            Label16.Visible = true;
            check = true;
        }
        if (txtContact.Text.Equals(""))
        {
            Label18.Visible = true;
            check = true;
        }
        else if (!Regex.IsMatch(txtContact.Text, @"^\d{9,10}$"))
        {
            Label27.Visible = true;
            check = true;
        }
        if (txtState.Text.Equals(""))
        {
            Label20.Visible = true;
            check = true;
        }
        if (txtCountry.Text.Equals(""))
        {
            Label22.Visible = true;
            check = true;
        }
        if (txtCity.Text.Equals(""))
        {
            Label24.Visible = true;
            check = true;
        }

        if (!check)
        {
            this.AddEmployees(txtDesignation.Text, txtJoinDate.Text, Convert.ToDecimal(txtSalary.Text), txtFirstName.Text, txtLastName.Text, txtUserName.Text,this.EncryptString(txtPassword.Text,"Password"), txtAddress.Text, txtContact.Text, txtState.Text, txtCountry.Text, txtCity.Text);
        }

    }
    public void AddEmployees(String Designation, String Joindate, decimal Salary, String FirstName, String LastName, String UserName, String PassWord, String Address, String ContactNo, String State, String Country, String City)
    {
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("Insert into EmpRegister(Designation,Joindate,Salary,FirstName,LastName,UserName,PassWord,Address,ContactNo,State,Country,City) values(@Designation,@Joindate,@Salary,@FirstName,@LastName,@UserName,@PassWord,@Address,@ContactNo,@State,@Country,@City)", sqlConnect);
        sqlCommand.Parameters.AddWithValue("@Designation", Designation);
        sqlCommand.Parameters.AddWithValue("@Joindate", Joindate);
        sqlCommand.Parameters.AddWithValue("@Salary", Salary);
        sqlCommand.Parameters.AddWithValue("@FirstName", FirstName);
        sqlCommand.Parameters.AddWithValue("@LastName", LastName);
        sqlCommand.Parameters.AddWithValue("@UserName", UserName);
        sqlCommand.Parameters.AddWithValue("@PassWord", PassWord);
        sqlCommand.Parameters.AddWithValue("@Address", Address);
        sqlCommand.Parameters.AddWithValue("@ContactNo", ContactNo);
        sqlCommand.Parameters.AddWithValue("@State", State);
        sqlCommand.Parameters.AddWithValue("@Country", Country);
        sqlCommand.Parameters.AddWithValue("@City", City);
        sqlCommand.ExecuteNonQuery();
        sqlConnect.Close();

        txtDesignation.Text = "";
        txtAddress.Text = "";
        txtCity.Text = "";
        txtContact.Text = "";
        txtCountry.Text = "";
        txtFirstName.Text = "";
        txtJoinDate.Text = "";
        txtLastName.Text = "";
        txtPassword.Text = "";
        txtSalary.Text = "";
        txtState.Text = "";
        txtUserName.Text = "";
        success.Visible = true;
    }
    public Boolean CheckExistingUser(String name)
    {
        String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
        SqlConnection sqlConnect = new SqlConnection(connect);
        sqlConnect.Open();
        SqlCommand sqlCommand = new SqlCommand("select userName from EmpRegister", sqlConnect);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        while (reader.Read())
        {
            String userName = reader.GetString(0);
            if (userName.Equals(name))
            {
                sqlConnect.Close();
                return true;
            }
        }
        sqlConnect.Close();
        return false;
    }
    public void SetVisible()
    {
        Label2.Visible = false;
        Label4.Visible = false;
        Label6.Visible = false;
        Label8.Visible = false;
        Label10.Visible = false;
        Label12.Visible = false;
        Label14.Visible = false;
        Label16.Visible = false;
        Label18.Visible = false;
        Label20.Visible = false;
        Label22.Visible = false;
        Label24.Visible = false;
        Label25.Visible = false;
        Label26.Visible = false;
        Label27.Visible = false;
        success.Visible = false;
    }
    public string EncryptString(string Message, string Passphrase)
    {
        byte[] Results;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;
        byte[] DataToEncrypt = UTF8.GetBytes(Message);
        try
        {
            ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
            Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
        }
        finally
        {
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }
        return Convert.ToBase64String(Results);
    }
}